
var type="";
$(document).ready(function () {
    type=getvl("type");
    init( callback, 1);
});


function init( callback, current) {
    $("#thislist").html("");
    $.ajax({
        type: "get",
        url: urlcore + "/api/newGoods/selectPage?current=" + current+"&type="+type ,
        dataType: "json",
        headers: {"Content-Type": "application/json;charset=UTF-8"},
        data: {},
        success: function (data) {//data是返回的hash,key之类的值，key是定义的文件名
            if (data.success == true) {
                callback(data.data.records);
                $('#thiscount').text(data.data.total);
                $("#pager").pager({
                    pagenumber: current,
                    pagecount: data.data.pages,
                    totalcount: data.data.total,
                    buttonClickCallback: PageClick
                });
            } else if (data.code == 'OVERTIME') {
                var thisUrl = window.location.href;
                if (thisUrl.indexOf('login.html') <= -1) {
                    top.window.location.href = "login.html";
                }
            } else {
                alert(data.msg);
            }
        },
        error: function () {
            alert("查询失败！");
        }
    });

}


/*//回调函数
PageClick = function (pageclickednumber) {
    var type = getvl("goodsType") == "" ? 0 : getvl("goodsType");
    init(type, callback, pageclickednumber);
}*/
//回调函数
PageClick = function (pageclickednumber) {
    init( callback,pageclickednumber);
}
var callback = function (data) {
    var html = "", goodsName = "", goodsType = "", goodsUrl = "", markerPrice = "", returnRate = "", returnRateY = "",
        goodsStatus = "", gmtDatetime = "", id = "";
    var goodsStatusShow = "";//显示的商品状态
    var changgeStatusOptBtn = "";
    for (var i = 0; i < data.length; i++) {
        id = data[i].id;
        goodsName = data[i].goods.goodsName;
        goodsUrl = data[i].goods.goodsUrl != null ? data[i].goods.goodsUrl : "";
        mPrice = data[i].goods.mPrice;
        returnRate = data[i].goods.returnRate;
        postage = data[i].goods.postage;
        /*邮费类型（1 江浙沪包邮 2  满九十九包邮 3  七块钱邮费  4 全国包邮（港澳台地区、西藏、新疆、内蒙古、宁夏、青海等地区除外））*/
/*        if (postage == undefined) {
            postage = "";
        } else if (postage == 1) {
            postage = "江浙沪包邮";
        } else if (postage == 2) {
            postage = "满88包邮";
        } else if (postage == 3) {
            postage = "5块钱邮费"
        } else if (postage == 4) {
            postage = "全国包邮（港澳台地区、西藏、新疆、内蒙古、宁夏、青海等地区除外）";
        }*/

        if (data[i].goods.goodsType == 0) {
            goodsType = "蜜哆哆";
        } else if (data[i].goods.goodsType == 1) {
            goodsType = "蜜雅阁";
        } else if (data[i].goods.goodsType == 2) {
            goodsType = "品牌馆";
        } else {
            goodsType = "";
        }

        returnRateY = data[i].goods.returnRatey;
        goodsStatus = data[i].goods.goodsStatus;

        if (goodsStatus == 1) {
            goodsStatusShow = "尚未发布";
            changgeStatusOptBtn = "   \t\t\t<a class=\"btn btn-primary btn-xs\" href=\"javascript:;\" onclick=\"changgeToStatus(" + id + "," + "2" + ")\"> 发布上架</a>\n";
        } else if (goodsStatus == 2) {
            goodsStatusShow = "已发布上架";
            changgeStatusOptBtn = "   \t\t\t<a class=\"btn btn-primary btn-xs\" href=\"javascript:;\" onclick=\"changgeToStatus(" + id + "," + "3" + ")\"> 下架</a>\n";

        } else if (goodsStatus == 3) {
            goodsStatusShow = "已下架";
            changgeStatusOptBtn = "   \t\t\t<a class=\"btn btn-primary btn-xs\" href=\"javascript:;\" onclick=\"changgeToStatus(" + id + "," + "2" + ")\"> 重新上架</a>\n";
        } else {
            goodsStatusShow = "";
        }
        var isHome = "";
        var isHomec = "";
        if(data[i].isHome==1){
            isHome = "是";
            isHomec = "   \t\t\t<a class=\"btn btn-primary btn-xs\" href=\"javascript:void(0);\" onclick=\"updIsHome(" + id +","+"0"+ ")\"> 取消首页显示</a>\n";
        }else{
            isHome = "否";
            isHomec = "  \t\t\t<a class=\"btn btn-primary btn-xs\" href=\"javascript:void(0);\" onclick=\"updIsHome(" + id +","+"1"+ ")\">设置首页显示</a>\n" ;
        }
        gmtDatetime = data[i].gmtDatetime != null ? new Date(data[i].gmtDatetime).pattern("yyyy-MM-dd hh:mm:ss") : "";
        html += "  <tr id=\"" + id + "\">\n" +
            "  \t\t<td>" + (i + 1) + "</td>\n" +
            "  \t\t<td>" + goodsName + "</td>\n" +
            "  \t\t<td>" + mPrice + "</td>\n" +
            "\t\t <td class=\"footable-visible\"><img width=\"40px\" height=\"40px\" src=\"" + goodsUrl + "\"></td>\n" +
            "  \t\t<th>" + returnRate + "</th>\n" +
            "  \t\t<th>" + returnRateY + "</th>\n" +
            // "  \t\t<th>" + postage + "</th>\n" +
            "  \t\t<th>" + goodsType + "</th>\n" +
            "  \t\t<th>" + goodsStatusShow + "</th>\n" +
            "  \t\t<th>" + gmtDatetime + "</th>\n" +
            "  \t\t<th>" + isHome + "</th>\n" +
            "  \t\t<td>\n" +
            "   \t\t\t<a class=\"btn btn-danger btn-xs\" href=\"javascript:;\" onclick=\"doDelete(" + id + ")\"> 删除</a>\n" + isHomec+

            //"  \t\t\t<a class=\"btn btn-primary btn-xs\" href=\"javascript:void(0);\" onclick=\"isHome(" + id + ")\">设置首页显示</a>\n" +
            //"   \t\t\t<a class=\"btn btn-primary btn-xs\" href=\"javascript:void(0);\" onclick=\"isHome(" + id + ")\"> 取消首页显示</a>\n" +

            "  \t\t</td>\n" +
            "  \t</tr>";
    }
    $("#thislist").append(html);
}

//设置和取消首页显示
function updIsHome(id, type) {
    $.ajax({
        url: urlcore + "/api/newGoods/updIsHome?id=" + id + "&isHome="+type,
        type: "get",
        dataType: "json",
        headers: {"Content-Type": "application/json;charset=UTF-8"},
        success: function (cbdata) {
            location.reload();
        },
        error: function (cbdata) {
            alert("更新状态失败");
        }
    })

}


//刪除
function doDelete(id) {

    $.ajax({
        type: "GET",
        url: urlcore + "/api/newGoods/deleteNewGoods?id=" + id,
        dataType: "json",
        headers: {"Content-Type": "application/json;charset=UTF-8"},
        data: {},
        success: function (data) {//data是返回的hash,key之类的值，key是定义的文件名
            if (data.success == true) {
                location.reload();
            } else {
                alert(data.msg);
            }
        },
        error: function () {
            alert("删除失败");
        }
    });
}

//更改商品状态
function changgeToStatus(id, targetStatus) {
    if (targetStatus == 3) {
        if (!confirm("确认下架该商品吗"))
            return;
    }
    $.ajax({
        url: urlcore + "/api/goods/update",
        type: "post",
        dataType: "json",
        headers: {"Content-Type": "application/json;charset=UTF-8"},
        data: JSON.stringify({
            id: id,
            goodsStatus: targetStatus
        }),
        success: function (cbdata) {
            location.reload();
        },
        error: function (cbdata) {
            alert("更新状态失败");
        }
    })

}


//搜索
function searchuser() {
    var type = $('#goods_type option:selected').val();//选中的值
    init(type, callback, 1);
}


